﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Zhuang.WinForm.AppCode.Common;
using Zhuang.Utility.Common.DataBase;

namespace Zhuang.WinForm.AuthorityManager
{
    public partial class SelectMenu : Form
    {
        string userid = "";

        public SelectMenu(string userid)
        {
            InitializeComponent();
            this.userid = userid;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            doSearch();
        }

        private void doSearch()
        {
            string condiction = "";
            string strSql = "";
            condiction = tbCondiction.Text.Trim();
            strSql = "select menuid,menuname from App_Menu where menuid not in(select menuid from App_UserToMenu where userid ="+userid+")";

            try {

                dataGridView1.DataSource= SqlHelper.Query(strSql).Tables[0];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count < 1)
            {
                MessageBox.Show("没有选择任何菜单！");
                return;
            }

            try {

                foreach (DataGridViewRow dgvr in dataGridView1.SelectedRows)
                {
                    string menuid = dgvr.Cells["menuid"].Value.ToString().Trim();
                    string strSql = "insert into App_UserToMenu(userid,menuid) values(" + userid + "," + menuid + ")";
                    SqlHelper.ExecuteSql(strSql);
                }

                this.DialogResult = DialogResult.OK;
                this.Close();
            
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void SelectMenu_Load(object sender, EventArgs e)
        {
            doSearch();
        }
    }
}
